import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xindong/common/xindong_sezhi.dart';

// 搜索框
class XinDongSearchTextField extends StatelessWidget {
  final bool digitsOnly; // 限制只能输入数字
  final String placeholder;
  final TextEditingController? controller;
  final ValueChanged<String>? onChanged;
  final ValueChanged<String>? onSubmitted;

  const XinDongSearchTextField(
      {Key? key, this.digitsOnly = false, this.controller, this.onChanged, this.onSubmitted, this.placeholder = "请输入"})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextField(
      maxLines: 1,
      minLines: 1,
      controller: controller,
      inputFormatters: digitsOnly ? [FilteringTextInputFormatter.digitsOnly] : [],
      textAlignVertical: TextAlignVertical.center,
      textInputAction: TextInputAction.search,
      style: const TextStyle(fontSize: 15, color: DZColors.titleColor),
      keyboardType: digitsOnly ? TextInputType.number : TextInputType.text,
      decoration: InputDecoration(
        // 设置这个之后，居中
        contentPadding: const EdgeInsets.only(left: 0, right: 0, top: 0, bottom: 0),
        // 占位符
        hintText: placeholder,
        // 占位符样式
        hintStyle: const TextStyle(color: DZColors.placeholderColor, fontSize: 16.0, fontWeight: FontWeight.normal),
        // 前缀左侧视图
        prefixIcon: const Padding(
          padding: EdgeInsets.only(left: 10, right: 2.0),
          child: Icon(Icons.search, color: DZColors.placeholderColor),
        ),
        prefixIconConstraints: const BoxConstraints(maxWidth: 44),

        // 边框、圆角
        border: const OutlineInputBorder(borderSide: BorderSide.none),
      ),
      onChanged: onChanged,
      onSubmitted: onSubmitted,
    );
  }
}
